// pages/home/commentDetail/index.js
import {
  home
} from '../../../api/index'
import {
  formatTime
} from "../../../utils/util";
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    topDistance: "",
    topDistance_2: "",
    bottomDistance: "",
    PageIndex: 1,
    PageSize: 10,
    loading: false, //是否正在加载数据
    ID: "661a314745581d5cb0d305bd",
    commentList: [],
    TotalCount: 1
  },
  //返回上一级
  goBack() {
    console.log('返回')
    wx.navigateBack();
  },
  // 获取数据
  async getData() {
    // 如果正在加载数据，则直接返回，避免重复加载
    if (this.data.loading) {
      return;
    }
    // 设置加载中的标志
    this.setData({
      loading: true,
    });
    if (this.data.commentList.length === this.data.TotalCount) {
      return
    }
    const {
      PageIndex,
      PageSize,
      ID
    } = this.data
    let res = await home.commentsortbyhot({
      PageIndex,
      PageSize,
      ID
    })
    res.Result.Datas.forEach(item => {
      // 设置每个一级评论下的二级评论数据的初始状态
      item.showSecondary = false
      item.secondaryCommentList = []
    })
    console.log(res.Result.Datas, '一级评论的数组')
    this.setData({
      commentList: this.data.commentList.concat(res.Result.Datas),
      TotalCount: res.Result.TotalCount,
      PageIndex: this.data.PageIndex + 1,
      loading: false
    })

  },
  // 给一级评论点赞
  Ilike(e) {
    // console.log(e.currentTarget.dataset)
    const _this = this
    const ID = e.currentTarget.dataset.id
    const IsLike = e.currentTarget.dataset.islike
    let index = e.currentTarget.dataset.index //一级评论的index
    const obj = {
      LikeID: ID
    }
    home[IsLike ? 'removelikeformcomment' : 'addlikeformcomment'](obj).then((res) => {
      if (res.StatusCode === 200) {
        const num = IsLike ? this.data.commentList[index].LikeCount -= 1 : this.data.commentList[index].LikeCount += 1
        let comment = this.data.commentList[index];
        comment.IsLike = !comment.IsLike
        comment.LikeCount = num
        this.setData({
          commentList: this.data.commentList
        })
      }
    }).catch(err => {
      if (err.statusCode === 401) {
        // 未登录
        _this.shudLogin();
      }
    })
  },
  // 展开、收起操作
  async toggleShowSecondary(e) {
    const {
      index,
      id
    } = e.currentTarget.dataset
    let correctComment = this.data.commentList[index]
    correctComment.showSecondary = !correctComment.showSecondary
    if (correctComment.showSecondary) {
      // 获取二级评论
      let res = await home.getsecondcomment({
        "PageIndex": 1,
        "PageSize": 100,
        "ParentId": id
      })
      correctComment.secondaryCommentList = res.Result.Datas
    }
    this.setData({
      commentList: this.data.commentList
    })
    // console.log(this.data.commentList,'设置后的commentList')
  },
  // 给二级评论点赞
  secondaryLike(e) {
    // index:一级评论的下标，secondaryindex：一级评论下二级评论的下标  islike：二级评论是否点赞 id：二级评论的id
    const {
      index,
      islike,
      secondaryindex,
      id
    } = e.currentTarget.dataset
    const obj = {
      LikeID: id
    }
    home[islike ? 'removelikeformcomment' : 'addlikeformcomment'](obj).then((res) => {
      if (res.StatusCode === 200) {
        const num = islike ? this.data.commentList[index].secondaryCommentList[secondaryindex].LikeCount -= 1 : this.data.commentList[index].secondaryCommentList[secondaryindex].LikeCount += 1
        let comment = this.data.commentList[index].secondaryCommentList[secondaryindex];
        comment.IsLike = !comment.IsLike
        comment.LikeCount = num
        this.setData({
          commentList: this.data.commentList
        })
      }
    }).catch(err => {
      if (err.statusCode === 401) {
        // 未登录
        _this.shudLogin();
      }
    })
  },
  //去登录
  shudLogin() {
    const popup = this.selectComponent('#popupid');
    popup.setData({
      visible: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const _this = this
    wx.getSystemInfo({
      success: res => {
        let systemInfo = wx.getSystemInfoSync()
        let info = wx.getMenuButtonBoundingClientRect()
        _this.setData({
          navTop: (res.statusBarHeight) + "px",
          navTop_2: (res.statusBarHeight) * 2 + "px",
          navHeight: (info.top - systemInfo.statusBarHeight) * 2 + info.height
        })
      },
      fail(err) {
        console.log(err);
      }
    })
    this.setData({
      ID: options.ID
    })
    this.getData()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log('触底')
    this.getData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})